import re

import pymongo
from bson import ObjectId

from database.db import listcol


def delete_one(c):
    global url
    print(dict(c))
    title = c['title']
    try:
        name = c['name']
    except:
        name = re.findall("《(.*)》", title)[0]
        listcol.update_one(c, {'$set': {'name': name}})
    url = c['url']
    if listcol.count_documents({'url': url}) > 1:
        repeat_movies = listcol.find({'url': url}).sort('updatedAt', pymongo.DESCENDING)

        index = 0
        for m in repeat_movies:
            if index > 0:
                print('删除重复电影', dict(m))
                listcol.delete_one(dict(m))
            index += 1
    if listcol.count_documents({'title': title, 'name': name}) > 1:
        repeat_movies = listcol.find({'title': title, 'name': name}).sort('updatedAt', pymongo.DESCENDING)
        index = 0
        for m in repeat_movies:
            if index > 0:
                print('删除重复电影', dict(m))
                listcol.delete_one(dict(m))
            index += 1


def deltet_all():
    skip = 0
    while True:
        cursor = listcol.find({}).sort('updatedAt', pymongo.DESCENDING).limit(100).skip(skip * 100)
        movies = []
        for c in cursor:
            delete_one(c)

        skip += 1

if __name__ == '__main__':
    c = {'_id': ObjectId('60e8e6cca846e32764cbb61b'), 'duration': '度】60min*4', 'categories': '】英剧、惊悚', 'intro': '《癫狂之旅》讲述了一个黑暗的扭曲的故事，有时候朋友和敌人的界限是如此', 'category': {'_id': ObjectId('60d357c7eaf716a885c593e7'), 'url': 'https://www.hao6v.tv/mj/', 'category': '欧美剧'}, 'date': '2011-03-06', 'title': '迷你剧《癫狂之旅》第04集', 'name': '癫狂之旅', 'url': 'https://www.hao6v.tv/mj/2011-02-14/14092.html', 'isRecommend': 0, 'md5': '709ddacd7cf949a05f1cb88f4cbb9224', 'updatedAt': '2021-07-10 08:16:12', 'createdAt': '2021-07-10 08:16:12', 'detailed': 0, 'order': 14092, 'main_category': 'tv', 'sub_category': '欧美剧', 'images_url': ['http://thumbsnap.com/i/kvf8VByE.jpg', 'http://thumbsnap.com/i/1VDN8dIg.jpg', 'http://thumbsnap.com/i/uEJSEqkv.jpg', 'http://thumbsnap.com/i/BukPGHjV.jpg', 'http://thumbsnap.com/i/2mnULxwo.jpg', 'http://thumbsnap.com/i/q2czxGhq.jpg', 'http://i990.photobucket.com/albums/af25/snowbubblelf/01-12.jpg', 'http://i990.photobucket.com/albums/af25/snowbubblelf/02-12.jpg', 'http://i990.photobucket.com/albums/af25/snowbubblelf/03-12.jpg', 'http://i990.photobucket.com/albums/af25/snowbubblelf/04-10.jpg'], 'images': ['a92d2094758f22c9f8edf67f128a2714.jpg', 'a4142713c3dd6fc23124b7fa48804d05.jpg', '1092f37e1403a2c6d0a0cb37e0f94edd.jpg', '532fc5e87c86783696d8f2647387f5c9.jpg'], 'links': [], 'online': [], 'valid': 0}
    # delete_one(c)
    deltet_all()